<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="dialogAddOrEdit.visible"
    :width="900"
    :title="dialogAddOrEdit.title"
    draggable
    close-icon="CloseBold"
    @close="closeDialog(false)"
  >
    <el-form ref="addOrEditFormRef" v-loading="loading" :model="formData" :rules="rules" label-width="100" size="">
      <el-row justify="start" class="d_left">
        <el-col :span="12" class="">
          <el-form-item label="项目名称">
            <el-input v-model="projectInfo.name" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="项目编号">
            <el-input v-model="projectInfo.prjNum" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="流水号" prop="code">
            <el-input v-model="formData.code" disabled placeholder="自动生成" />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="">
          <el-form-item label="开户银行" prop="bankName">
            <el-input v-model="formData.bankName" disabled placeholder="请输入开户银行" />
            <!-- <el-select v-model="formData.bankType" :placeholder="'请选择'" filterable clearable>
              <el-option v-for="item in BankTypeTypes" :key="item.label" :label="item.label" :value="item.label" />
            </el-select> -->
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="支行名称" prop="bankSubbranch">
            <el-select
              v-model="formData.bankSubbranch"
              :placeholder="dialogAddOrEdit.detail === '详情' ? '' : '请选择'"
              filterable
              clearable
            >
              <el-option v-for="item in bankSubbranchs" :key="item.label" :label="item.label" :value="item.label" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">{{ dialogAddOrEdit.footBtn }}</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ProjectSpecialAccountEditForm } from "@/api/specialaccount/types";
import { CreateProjectSpecialAccount, GetProjectSpecialAccount } from "@/api/specialaccount/index";
import { getDicts } from "@/store/modules/dict";
import { FormRules } from "element-plus";

const props = defineProps({
  dialogAddOrEdit: {
    type: Object,
    default: () => ({
      visible: false,
      title: "专户申请",
      footBtn: "确认申请并打印申请书",
      id: 0,
    }),
  },
});

const { dialogAddOrEdit } = toRefs(props);

const projectInfo = JSON.parse(localStorage.getItem("project") ?? "{}");
const BankTypeTypes = getDicts("bankTypeTypes"); //银行类别
const bankSubbranchs = getDicts("bankSubbranchs"); //支行

const emit = defineEmits(["sendClose"]);

const loading = ref(false);
const formData = reactive<ProjectSpecialAccountEditForm>({
  bankName: "西藏银行股份有限公司",
  bankType: "7710001",
});
const addOrEditFormRef = ref(ElForm);

const rules = reactive<FormRules<ProjectSpecialAccountEditForm>>({
  // bankName: [{ required: true, message: "请选择开户银行", trigger: "blur" }],
});

onMounted(() => {
  loading.value = true;
  let id = 0;
  if (dialogAddOrEdit.value.id) {
    id = dialogAddOrEdit.value.id;
  }
  GetProjectSpecialAccount(id)
    .then((res) => {
      if (res.type == 0) {
        if (res.success) {
          Object.assign(formData, res.data);
          formData.bankName = "西藏银行股份有限公司";
          formData.bankType = "7710001";
        } else {
        }
      } else {
      }
    })
    .finally(() => {
      loading.value = false;
    });
});

/**
 * 关闭弹窗
 */
function closeDialog(query: boolean, data?: any) {
  emit("sendClose", query, data);
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  addOrEditFormRef.value.resetFields();
  addOrEditFormRef.value.clearValidate();
  formData.id = undefined;
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  addOrEditFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      loading.value = false;
      CreateProjectSpecialAccount(formData)
        .then((res) => {
          if (res.type == 0) {
            if (res.success) {
              closeDialog(true, res.data);
            } else {
            }
          } else {
          }
        })
        .finally(() => (loading.value = false));
    }
  });
}
</script>

<style lang="scss" scoped>
.d_left {
  margin-left: 10px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}
.dialog_footer {
  // display: flex;
  // justify-content: center;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-input__wrapper) {
  width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
